package main

import (
	"cow_golang/shared_mod/concurrency"
	"fmt"
	"time"
)

type Task struct {
	index      int
	resultChan chan int
}

func NewTask(resultChan chan int, index int) *Task {
	return &Task{
		index:      index,
		resultChan: resultChan,
	}
}

func (t *Task) Execute() {
	fmt.Printf("Worker 开始处理任务 %d\n", t.index)
	t.resultChan <- t.index
}

func main() {
	pool := concurrency.NewConcurrency(2)
	var result []int
	var resultChan = make(chan int, 5)
	// 顺序测试
	for i := 0; i < 5; i++ {
		j := i
		pool.AddTask("key1", NewTask(resultChan, j))
	}
	//// 并发测试
	//var wg sync.WaitGroup
	//wg.Add(2)
	//go func() {
	//	defer wg.Done()
	//	pool.AddTask("key2", concurrency.JobFunc(func() {
	//		fmt.Printf("Worker 开始处理任务 key2\n")
	//		time.Sleep(100 * time.Millisecond)
	//	}))
	//}()
	//go func() {
	//	defer wg.Done()
	//	pool.AddTask("key3", concurrency.JobFunc(func() {
	//		fmt.Printf("Worker 开始处理任务 key3\n")
	//		time.Sleep(100 * time.Millisecond)
	//	}))
	//}()
	//wg.Wait()
	//
	time.Sleep(3000 * time.Millisecond)
	close(resultChan)
	pool.Stop()
	// 去读结果
	for num := range resultChan {
		result = append(result, num)
	}

	for i := 0; i < 5; i++ {
		fmt.Printf("正确顺序, 期望 %d 实际 %d", i, result[i])
	}
}
